package com.dbl.oms.platform.facade.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.dbl.oms.common.enums.ErrorCode;
import com.dbl.oms.common.model.ResultData;
import com.dbl.oms.common.model.SuperApi;
import com.dbl.oms.common.model.SuperService;
import com.dbl.oms.platform.persistence.entity.SysUser;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 用户管理控制接口
 *
 * @author Ladd
 * @Date 2017-12-15 11:20:26
 */
 
@RestController
@RequestMapping(value = "/platform/UserMngService")
@Slf4j
@Api(tags = { "用户管理" })
public class UserMngServiceApi extends SuperApi {

	@Autowired
	SuperService<SysUser> sysUserService;

	/**
	 * 
	 * @param sysDict
	 * @return
	 */
	@ApiOperation(value = "保存数据")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public @ResponseBody ResultData<Boolean> insert(@RequestBody SysUser entity) {
		log.info("----------start-------");
		ResultData<Boolean> result = ResultData.getSuccessResult();
		try {
			result.setData(sysUserService.insert(entity));
		} catch (Exception e) {
			log.error("------------------", e);
			result.setCode(ErrorCode.UNKNOWN_EXCEPTION.getCode());
			result.setMsg(ErrorCode.UNKNOWN_EXCEPTION.getMsg());
		}
		log.info("----------end-------");
		return result;
	}
	
	/**
	 * 
	 * @param list
	 * @return
	 */
	@ApiOperation(value = "批量保存数据")
	@RequestMapping(value = "/insertBatch", method = RequestMethod.POST)
	public @ResponseBody ResultData<Boolean> insertBatch(@RequestBody List<SysUser> list) {
		log.info("----------start-------");
		ResultData<Boolean> result = ResultData.getSuccessResult();
		try {
			result.setData(sysUserService.insertBatch(list));
		} catch (Exception e) {
			log.error("------------------", e);
			result.setCode(ErrorCode.UNKNOWN_EXCEPTION.getCode());
			result.setMsg(ErrorCode.UNKNOWN_EXCEPTION.getMsg());
		}
		log.info("----------end-------");
		return result;
	}
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "根据主键ID删除数据")
	@RequestMapping(value = "/deleteById", method = RequestMethod.POST)
	public @ResponseBody ResultData<Boolean> deleteById(@RequestBody Integer id) {
		log.info("----------start-------");
		ResultData<Boolean> result = ResultData.getSuccessResult();
		try {
			result.setData(sysUserService.deleteById(id));
		} catch (Exception e) {
			log.error("------------------", e);
			result.setCode(ErrorCode.UNKNOWN_EXCEPTION.getCode());
			result.setMsg(ErrorCode.UNKNOWN_EXCEPTION.getMsg());
		}
		log.info("----------end-------");
		return result;
	}
	
	/**
	 * 
	 * @param entity
	 * @return
	 */
	@ApiOperation(value = "更新数据")
	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public @ResponseBody ResultData<Boolean> updateById(@RequestBody SysUser entity) {
		log.info("----------start-------");
		ResultData<Boolean> result = ResultData.getSuccessResult();
		try {
			result.setData(sysUserService.updateById(entity));
		} catch (Exception e) {
			log.error("------------------", e);
			result.setCode(ErrorCode.UNKNOWN_EXCEPTION.getCode());
			result.setMsg(ErrorCode.UNKNOWN_EXCEPTION.getMsg());
		}
		log.info("----------end-------");
		return result;
	}
	
	/**
	 * 
	 * @param list
	 * @return
	 */
	@ApiOperation(value = "批量更新数据")
	@RequestMapping(value = "/updateBatchById", method = RequestMethod.POST)
	public @ResponseBody ResultData<Boolean> updateBatchById(@RequestBody List<SysUser> list) {
		log.info("----------start-------");
		ResultData<Boolean> result = ResultData.getSuccessResult();
		try {
			result.setData(sysUserService.updateBatchById(list));
		} catch (Exception e) {
			log.error("------------------", e);
			result.setCode(ErrorCode.UNKNOWN_EXCEPTION.getCode());
			result.setMsg(ErrorCode.UNKNOWN_EXCEPTION.getMsg());
		}
		log.info("----------end-------");
		return result;
	}
	
	
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "根据主键ID获取详细信息")
	@RequestMapping(value = "/findById", method = RequestMethod.GET)
	public @ResponseBody ResultData<SysUser> findById(@RequestBody Integer id) {
		log.info("----------start-------");
		ResultData<SysUser> result = ResultData.getSuccessResult();
		try {
			result.setData(sysUserService.selectById(id));
		} catch (Exception e) {
			log.error("------------------", e);
			result.setCode(ErrorCode.UNKNOWN_EXCEPTION.getCode());
			result.setMsg(ErrorCode.UNKNOWN_EXCEPTION.getMsg());
		}
		log.info("----------end-------");
		return result;
	}

	/**
	 * 
	 * @param page
	 * @return
	 */
	@ApiOperation(value = "根据分页查询参数获取，分页查询结果")
	@RequestMapping(value = "/queryList", method = RequestMethod.POST)
	public @ResponseBody ResultData<Page<SysUser>> queryList(@RequestBody Page<SysUser> page) {
		
		log.info("----------start-------");
		ResultData<Page<SysUser>> result = ResultData.getSuccessResult();
		try {
			result.setData(sysUserService.selectPage(page));
		} catch (Exception e) {
			log.error("------------------", e);
			result.setCode(ErrorCode.UNKNOWN_EXCEPTION.getCode());
			result.setMsg(ErrorCode.UNKNOWN_EXCEPTION.getMsg());
		}
		log.info("----------end-------");
		return result;
	}
}
